dojo.provide("miaigo.view.board.Board");

dojo.require("dijit._Widget");
dojo.require("dijit._Templated");

dojo.require("miaigo.view.board.Cross");
dojo.require("miaigo.utilities");

dojo.declare(
    "miaigo.view.board.Board",
    [dijit._Widget, dijit._Templated],
    {
	templateString:
	    "<div><div dojoAttachPoint='containerNode'>" +
	        "<div dojoAttachPoint='boardContainer' class='board'>" +
	            "<table dojoAttachPoint='boardBackgroundTable' rules='all' cellspacing='0' cellpadding='0'>" +
                        "<tbody dojoAttachPoint='boardBackgroundBody'></tbody>" +
	            "</table>" +
	            "<div dojoAttachPoint='crossContainer' class='cross-container'></div>" +
	        "</div>" +
	    "</div></div>",

	boardSize: 19,
	stoneSize: 32,
	boardMargin: 24,

	crosses: null,

	postCreate: function()
	{
	    this.inherited(arguments);
	},

	startup: function()
	{
	    if (this._started)
		return;

	    this.inherited(arguments);

	    this._buildBackground();
	    this._buildCrosses();
	},

	getStoneSize: function()
	{
	    return this.stoneSize;
	},

	setStoneSize: function(stoneSize)
	{
	    this.stoneSize = stoneSize;
	    this.boardMargin = stoneSize * 3 / 4;

	    // resize all background cells
	    for (var y = 0; y < this.boardSize - 1; y++)
	    {
		for (var x = 0; x < this.boardSize - 1; x++)
		{
		    var cell = this._getCellNode(x, y);
		    this._resizeBkCell(cell);
		}
	    }

	    // resize container
	    this._resizeContainer();

	    // resize all crosses
	    for (i in this.crosses)
	    {
		this._resizeCross(this.crosses[i]);
	    }
	},

	_createBkRow: function(bkNode)
	{
	    return dojo.create("tr", null, bkNode);
	},

	_createBkCell: function(rowNode)
	{
	    var cellAttr = {};

	    if (dojo.isIE)
	    {
		cellAttr.innerHTML = "&nbsp;";
	    }

	    var cell = dojo.create("td", cellAttr, rowNode);
	    this._resizeBkCell(cell);

	    return cell;
	},

	_resizeBkCell: function(cell)
	{
	    dojo.style(cell, "width", (this.stoneSize - 1) + "px");
	    dojo.style(cell, "height", this.stoneSize + "px");
	},

	_resizeContainer: function()
	{
	    var crossContainerSize = this.boardSize * this.stoneSize;
	    var crossContainerOffset = this.boardMargin - this.stoneSize / 2;
	    var containerSize = crossContainerSize + 2 * crossContainerOffset;

	    if (dojo.isSafari)
	    {
		dojo.style(this.boardContainer, "padding-left", this.boardMargin + "px");
		dojo.style(this.boardContainer, "padding-top", this.boardMargin + "px");
		dojo.style(this.boardContainer, "width", containerSize + "px");
		dojo.style(this.boardContainer, "height", containerSize - this.boardMargin + "px");
	    }
	    else
	    {
		dojo.style(this.boardBackgroundTable, "margin", this.boardMargin + "px");
		dojo.style(this.boardContainer, "width", containerSize + "px");
		dojo.style(this.boardContainer, "height", containerSize + "px");

		dojo.style(this.containerNode, "width", containerSize + "px");
		dojo.style(this.containerNode, "height", containerSize + "px");
	    }

	    dojo.style(this.crossContainer, "left", crossContainerOffset + "px");
	    dojo.style(this.crossContainer, "top", crossContainerOffset + "px");
	    dojo.style(this.crossContainer, "width", crossContainerSize + "px");
	    dojo.style(this.crossContainer, "height", crossContainerSize + "px");
	},

	_buildBackground: function()
	{
	    // build a fixed layout table with border as board background
	    for (var y = 1; y < this.boardSize; y++)
	    {
		var row = this._createBkRow(this.boardBackgroundBody);
		for (var x = 1; x < this.boardSize; x++)
		{
		    this._createBkCell(row);
		}
	    }

	    this._resizeContainer();
	    
	    // TODO: star marks
	},

	_buildCrosses: function()
	{
	    this.crosses = {};

	    for (var y = 0; y < this.boardSize; y++)
	    {
		for (var x = 0; x < this.boardSize; x++)
		{
		    this._buildCross(x, y);
		}
	    }
	},

	_resizeCross: function(cross)
	{
	    var cell = null;
	    var crossLeft = 0;
	    var crossTop = 0;

	    if (cross.x < this.boardSize - 1)
	    {
		if (cross.y < this.boardSize - 1)
		{
		    cell = this._getCellNode(cross.x, cross.y);
		    crossLeft = cell.offsetLeft - this.stoneSize / 2;
		    crossTop = cell.offsetTop - this.stoneSize / 2;
		}
		else
		{
		    cell = this._getCellNode(cross.x, cross.y-1);
		    crossLeft = cell.offsetLeft - this.stoneSize / 2;
		    crossTop = cell.offsetTop + this.stoneSize / 2;
		}
	    }
	    else
	    {
		if (cross.y < this.boardSize - 1)
		{
		    cell = this._getCellNode(cross.x-1, cross.y);
		    crossLeft = cell.offsetLeft + this.stoneSize / 2;
		    crossTop = cell.offsetTop - this.stoneSize / 2;
		}
		else
		{
		    cell = this._getCellNode(cross.x-1, cross.y-1);
		    crossLeft = cell.offsetLeft + this.stoneSize / 2;
		    crossTop = cell.offsetTop + this.stoneSize / 2;
		}
	    }

	    var crossContainerOffset = this.boardMargin - this.stoneSize / 2;
	    var offset = this.boardMargin - crossContainerOffset - 2;

	    dojo.style(cross.domNode, "left", crossLeft + offset + "px");
	    dojo.style(cross.domNode, "top", crossTop + offset + "px");

	    cross.setSize(this.stoneSize);
	},

	_buildCross: function(x, y)
	{
	    var cross = new miaigo.view.board.Cross( {board: this, x: x, y: y} );
	    var key = miaigo.utilities.pointToString( {x:x, y:y} );
	    this.crosses[key] = cross;

	    this.crossContainer.appendChild(cross.domNode);
	    this._resizeCross(cross);
	},

	_getCellNode: function(x, y)
	{
	    return this.boardBackgroundBody.rows[y].cells[x];
	},

	onCrossClicked: function(cross)
	{
	    console.debug("Cross Clicked: [" + cross.x + " ," + cross.y + "]");
	},

	placeStone: function(stone, x, y)
	{
	    var key = miaigo.utilities.pointToString( {x: x, y: y} );
	    var cross = this.crosses[key];

	    if (cross)
	    {
		cross.placeStone(stone);
	    }
	},

	placeStoneBlock: function(/* miaigo.model.StoneBlock */block)
	{
	    if (block)
	    {
		var stone = block.getColor();
		dojo.forEach
		(
		    block.getAllPoints(),
		    function(point)
		    {
			var key = miaigo.utilities.pointToString(point);
			var cross = this.crosses[key];
			if (cross)
			{
			    cross.placeStone(stone);
			}
		    },
		    this
		);
	    }
	},

	removeStone: function(x, y)
	{
	    var key = miaigo.utilities.pointToString( {x: x, y: y} );
	    var cross = this.crosses[key];

	    if (cross)
	    {
		cross.removeStone();
	    }
	},

	clearBoard: function()
	{
	    for (var key in this.crosses)
	    {
		this.crosses[key].removeStone();
	    }
	}
    });